﻿<script type="text/javascript">


function alternateRows(){
    $('tr.data_row:even').addClass('even_row');
    $('tr.data_row:odd').addClass('odd_row');
}

$(document).ready(function() {
    $('#find_meetings_button').click(function() {
        $.post('/player/findMeeting.rails', {'id': $('#id').val(), 'venueId': $('#venueId').val(), 'activityId': $('#activityId').val()}, function(data) {
            $('.found_row').remove();
      
            $.each(data.Meetings, function(index, meeting){
                var html = '<tr class="data_row found_row">';
                html += '<td align="center">' + meeting['Activity'] + '</td>'; 
                html += '<td align="center">' + meeting['Description'] + '</td>'; 
                html += '<td align="center">' + meeting['Venue'] + '</td>'; 
                html += '<td align="center">' + meeting['Date'] + '</td>'; 
                html += '<td align="center">' + meeting['Host'] + '</td>';
                html += '<td align="center"><button class="join_meeting" onclick="joinMeeting(' + meeting['Id'] + ', ' + data.PlayerId + ')">Join Meeting</button></td>';
		        html += '</tr>'; 
                $('#found_meetings_header_row').after(html);
            });
            alternateRows();          
        }, "json");
    });
});

function addNewTableToMyTables(data){        
        $.each(data.Meetings, function(index, meeting){
            var html = '<tr class="data_row">';
            html += '<td align="center">' + meeting['Activity'] + '</td>'; 
            html += '<td align="center">' + meeting['Description'] + '</td>'; 
            html += '<td align="center">' + meeting['Venue'] + '</td>'; 
            html += '<td align="center">' + meeting['Date'] + '</td>'; 
            html += '<td align="center">' + meeting['Host'] + '</td>';
            html += '<td align="center">' + '' + '</td>';            
            html += '</tr>';                 
            // need to add html - but not to the following location 
            $('#my_meetings tr.my_meetings_row').after(html);
        });
        
    }

function joinMeeting(meetingId, playerId){
    $.post('/player/signUp.rails', {'id' : playerId, 'meetingId' : meetingId}, addNewTableToMyTables, "json");
    alternateRows();
}
</script>